﻿namespace EveKillboard
{
    using System.Web.Mvc;
    using System.Globalization;
    using System.Linq;
    using System.Threading;
    using System.Diagnostics;

    public class LocalizedController : Controller
    {
        protected  internal readonly TraceSource Trace = new TraceSource("Controller");

        private bool _isInGame;
        private bool _isTrusted;
        private string _characterName;
        private int _characterId;
        private string _corporationName;
        private int _corporationId;
        private string _allianceName;
        private int _allianceId;
        
        protected override void ExecuteCore()
        {
            if(Trace.Switch.ShouldTrace(TraceEventType.Verbose))
                Trace.TraceEvent(TraceEventType.Verbose, 0,"New request from IP:{0} Name:{1} UserAgent:{2}", Request.UserHostAddress, Request.UserHostName, Request.UserAgent);
            
            if (Request.UserLanguages != null && Request.UserLanguages.Any())
            {
                var culture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
                Thread.CurrentThread.CurrentUICulture = culture;
                Thread.CurrentThread.CurrentCulture = culture;

                if(Trace.Switch.ShouldTrace(TraceEventType.Verbose))
                    Trace.TraceEvent(TraceEventType.Verbose, 0, "Current culture: {0}", culture);
            }

            try
            {
                _isInGame = !string.IsNullOrEmpty(Request.UserAgent) && Request.UserAgent.Contains("EVE-IGB");
                if (_isInGame)
                {
                    _isTrusted = Request.Headers["EVE_TRUSTED"] == "Yes";
                    if(_isTrusted)
                    {
                        _characterName = Request.Headers["EVE_CHARNAME"];
                        _characterId = string.IsNullOrEmpty(_characterName) ? 0 : int.Parse(Request.Headers["EVE_CHARID"]);
                        _corporationName = Request.Headers["EVE_CORPNAME"];
                        _corporationId = string.IsNullOrEmpty(_corporationName) ? 0 : int.Parse(Request.Headers["EVE_CORPID"]);
                        _allianceName = Request.Headers["EVE_ALLIANCENAME"];
                        _allianceId = string.IsNullOrEmpty(_allianceName) ? 0 : int.Parse(Request.Headers["EVE_ALLIANCEID"]);
                    }
                }
            }
            catch (System.Exception exp)
            {
                Trace.TraceEvent(TraceEventType.Warning, 0, "Unable to read ingame details. Exception \"{0}\" Details: {1}", exp.GetType(), exp.Message);
            }

            base.ExecuteCore();
        }
        
        public bool IsInGame
        {
            get { return _isInGame; }
        }
        public bool IsTrusted
        {
            get { return _isTrusted; }
        }
        public string CharacterName
        {
            get { return _characterName; }
        }
        public int CharacterId
        {
            get { return _characterId; }
        }
        public string CorporationName
        {
            get { return _corporationName; }
        }
        public int CorporationId
        {
            get { return _corporationId; }
        }
        public string AllianceName 
        {
            get { return _allianceName; }
        }
        public int AllianceId
        {
            get { return _allianceId; }
        }
    }
}